# -*- coding: utf-8 -*-
"""
@Time    : 2025/3/15 11:15 
@Author  : ZhangShenao 
@File    : 消息过滤数据分析.py 
@Desc    : 
"""

from datetime import datetime

import matplotlib.pyplot as plt

# 数据
dates = ['2025-03-14', '2025-03-13', '2025-03-12', '2025-03-11', '2025-03-10', '2025-03-09', '2025-03-08']
openai_moderation = [0.80, 0.79, 0.74, 0.51, 0.51, 0.57, 0.56]
openrouter = [0.25, 0.10, 0.09, 0.11, 0.10, 0.07, 0.05]

# 将日期字符串转换为datetime对象以便排序
date_objects = [datetime.strptime(date, '%Y-%m-%d') for date in dates]

# 按日期排序
sorted_indices = sorted(range(len(date_objects)), key=lambda k: date_objects[k])
sorted_dates = [dates[i] for i in sorted_indices]
sorted_openai_moderation = [openai_moderation[i] for i in sorted_indices]
sorted_openrouter = [openrouter[i] for i in sorted_indices]

# 设置图形大小
plt.figure(figsize=(10, 6))

# 设置柱状图的位置
bar_width = 0.35
index = range(len(sorted_dates))

# 绘制柱状图
bars1 = plt.bar(index, sorted_openai_moderation, bar_width, label='OpenAI Moderation', color='b')
bars2 = plt.bar([i + bar_width for i in index], sorted_openrouter, bar_width, label='OpenRouter',
                color='g')


# 添加数值标签
def add_labels(bars):
    for bar in bars:
        yval = bar.get_height()
        plt.text(bar.get_x() + bar.get_width() / 2, yval, round(yval, 2), va='bottom')  # va: vertical alignment


add_labels(bars1)
add_labels(bars2)

# 添加标签和标题
plt.xlabel('date')
plt.ylabel('filter ratio(%)')
plt.title('Message Filtering Data Analysis(past 7-days)')
plt.xticks([i + bar_width / 2 for i in index], sorted_dates, rotation=45)
plt.legend()

# 显示图形
plt.tight_layout()
plt.show()
